//
//  makeSinTable.c
//  
//
//  Created by Bram Drenthe on 13-12-13.
//
//

#include <stdio.h>
#include "qmath.h"
#include <math.h>

#define MULTIPLIER      7
#define PER_Q_DIV       166
#define SINTABLECOUNT   (4341)

//unsigned short sinTable[SINTABLECOUNT];

void printhex(void * a, int bytes)
{
    int i;
    for (i=bytes-1;i>=0;i--)
    {
        unsigned char c = *((unsigned char*)a+i);
        
        char a[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        
        printf("%c%c",a[(c&0xF0)>>4],a[c&0x0F]);
    }
}

void generateSinTable()
{
    int i;
    float f;
    printf("const unsigned short sinTable[%i] = {",SINTABLECOUNT);
    int overflow = 0;
    
    for (i=0;i<SINTABLECOUNT;i++)
    {
        f = sinf((float)i/(float)SINTABLECOUNT/2.0f*M_PI);
        
        q32_t sinq = qFromFloat(f);
        sinq = sinq&0xFFFF;
        if (i > 10 && sinq == 0)
        {
            sinq = 0xFFFF;
            if (overflow == 0) overflow = SINTABLECOUNT-i;
        }
        //printf("%i;%.16f;%.16f\n",i,f,floatFromQ(sinTable[i])*2);
        if (i != 0) printf(",");
//        printf("%f  ",(float)i/(float)SINTABLECOUNT);
        printf("%i", sinq);
    }
    printf("};\n");
    printf("#define SIN_OVERFLOW %i\n\n",overflow);
}

int main()
{
    generateSinTable();
    return 0;
}
